package com.thinkfit.jy.activity;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.common.lib.bean.BannerBean;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.ThemeUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

/**
 * 选择屏保
 */
public class SwitchScreenProtectActivity extends MainActivity {

    private int mMainStyle;

    private int switchScreenProtectNum = 6;

    RelativeLayout rlMain;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_switch_screen_protect;
    }

    public void init() {
        mMainStyle = DataManager.Companion.getInstance().getMainStyle();
//        if (mMainStyle == 3) {
//            setBackground(R.id.rootView, R.drawable.app_bg_xm);
//            setImage(R.id.ivHome, R.drawable.app_home_xm);
//        }
        rlMain = findViewById(R.id.rl_main);
        resetDate();
        setViewsOnClickListener(R.id.llStyle0, R.id.llStyle1, R.id.llStyle2, R.id.llStyle3,
                R.id.llStyle4, R.id.llStyle5,R.id.ll_sport_back);
        getPresenter().getAdvert();
        ArrayList<BannerBean> list = DataManager.Companion.getInstance().getBannerList();
        if (!list.isEmpty()) {
            BaseUtils.StaticParams.loadImage(this, 0, list.get(0).getUrl(), findViewById(R.id.ivBanner));
        }
        resetUI();
    }

    @Override
    public void onResume() {
        super.onResume();
        rlMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
    }

    private void resetUI() {
        int currentStyle = DataManager.Companion.getInstance().getScreenProtectStyle();
        for (int i = 0; i < switchScreenProtectNum; ++i) {
            ViewGroup view = findViewById(
                    getResources().getIdentifier("llItem" + i, "id", getPackageName())
            );
            view.setBackgroundResource(currentStyle == i ?
                    R.drawable.shape_stroke_whirt_30_000000_12 : R.drawable.shape_stroke_30_000000_12);
//            if (mMainStyle == 3) {
//                view.setBackgroundResource(currentStyle == i ?
//                        R.drawable.shape_88ffffff_5 : R.drawable.shape_2bffffff_5);
//                if (i < 4) {
//                    view.getChildAt(1).setBackgroundColor(getColor(com.common.lib.R.color.color_22_ff_ff_ff));
//                }
//            } else {
//                view.setBackgroundResource(currentStyle == i ?
//                        R.drawable.shape_4b494d_stroke_2e2d2e_6 : R.drawable.shape_121212_stroke_2e2d2e_6);
//                if (i < 4) {
//                    view.getChildAt(1).setBackgroundColor(Color.BLACK);
//                }
//            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_sport_back:
                finish();
                break;
            case R.id.llStyle0:
                DataManager.Companion.getInstance().saveScreenProtectStyle(0);
                resetUI();
                openActivity(ScreenProtectActivity.class);
                break;
            case R.id.llStyle1:
                DataManager.Companion.getInstance().saveScreenProtectStyle(1);
                resetUI();
                openActivity(ScreenProtectActivity.class);
                break;
            case R.id.llStyle2:
                DataManager.Companion.getInstance().saveScreenProtectStyle(2);
                resetUI();
                openActivity(ScreenProtectActivity.class);
                break;
            case R.id.llStyle3:
                DataManager.Companion.getInstance().saveScreenProtectStyle(3);
                resetUI();
                openActivity(ScreenProtectActivity.class);
                break;
            case R.id.llStyle4:
                DataManager.Companion.getInstance().saveScreenProtectStyle(4);
                resetUI();
                openActivity(ScreenProtectActivity.class);
                break;
            case R.id.llStyle5:
                DataManager.Companion.getInstance().saveScreenProtectStyle(5);
                resetUI();
                openActivity(ScreenProtectActivity.class);
                break;
        }
    }

    public void resetDate() {
        super.resetDate();
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.AM_PM) == Calendar.AM) {
            setText(R.id.tvCurrentTime1, BaseUtils.StaticParams.getNewText(now.get(Calendar.HOUR_OF_DAY))
                    + ":" + BaseUtils.StaticParams.getNewText(now.get(Calendar.MINUTE)) + " " + getString(R.string.app_am));
            setText(R.id.tvCurrentTime2, BaseUtils.StaticParams.getNewText(now.get(Calendar.HOUR_OF_DAY))
                    + ":" + BaseUtils.StaticParams.getNewText(now.get(Calendar.MINUTE)) + " " + getString(R.string.app_am));
        } else {
            setText(R.id.tvCurrentTime1, BaseUtils.StaticParams.getNewText(now.get(Calendar.HOUR_OF_DAY) - 12)
                    + ":" + BaseUtils.StaticParams.getNewText(now.get(Calendar.MINUTE)) + " " + getString(R.string.app_pm));
            setText(R.id.tvCurrentTime2, BaseUtils.StaticParams.getNewText(now.get(Calendar.HOUR_OF_DAY) - 12)
                    + ":" + BaseUtils.StaticParams.getNewText(now.get(Calendar.MINUTE)) + " " + getString(R.string.app_pm));
        }
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        setText(R.id.tvDate1, year + "/" + BaseUtils.StaticParams.getNewText(month) + "/" + BaseUtils.StaticParams.getNewText(day) + " " + getString(getResources().getIdentifier("app_weekday_" + weekDay, "string", getPackageName())));
        setText(R.id.tvDate2, year + "/" + BaseUtils.StaticParams.getNewText(month) + "/" + BaseUtils.StaticParams.getNewText(day) + " " + getString(getResources().getIdentifier("app_weekday_" + weekDay, "string", getPackageName())));
    }


    protected void showBottomUI() {

    }

    @Override
    public boolean isMainActivity() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.TIME_CHANGE)) {
            resetDate();
        } else {
            super.onReceive(map);
        }
    }
}